#region Code by Robert McLaws

//Original code by Telligent
//Last Updated: Robert McLaws
//Date: 22 Sept 2007

#endregion

#region Import Declarations

using System;
using System.Collections.Generic;
using System.Text;
using CommunityServer.Controls;
using CommunityServer.Components;
using System.Web.UI.WebControls;

#endregion

namespace CommunityServerStuff.Controls
{
    /// <summary>
    /// Single value control that provides access to CSLink data and links.
    /// </summary>
    /// <remarks>
    /// This version allows you to specify the ID property for the Hyperlink, for the purposes of CSS styling.
    /// The CS version only lets you name the parent container (typically an &lt;li&gt; tag.
    /// </remarks>
    public class CSLinkData : ObjectDataBase
    {

        #region Private Members

        private CSLink _link = null;

        #endregion

        #region Properties

        #region DataSource

        public override object DataSource
        {
            get
            {
                if (_link == null)
                    _link = CSControlUtility.Instance().GetCurrentBoundObject<CSLink>(this);

                return _link;
            }
            set
            {
                if (value is CSLink)
                    _link = value as CSLink;
                else
                    throw new InvalidCastException("DataSource must be a valid CSLink object");
            }
        }

        #endregion

        #region LinkTo

        /// <summary>
        /// If specified, the content will be linked to the specified CSLink-related location
        /// </summary>
        public CSLinkLinkTo LinkTo
        {
            get { return (CSLinkLinkTo)(ViewState["LinkTo"] ?? CSLinkLinkTo.Nothing); }
            set { ViewState["LinkTo"] = value; }
        }

        #endregion

        #region LinkId

        /// <summary>
        /// If specified, the content will be linked to the specified CSLink-related location
        /// </summary>
        public string LinkId
        {
            get { return (string)(ViewState["LinkId"] ?? String.Empty); }
            set { ViewState["LinkId"] = value; }
        }

        #endregion
        
        #endregion

        #region Methods

        protected override System.Web.UI.WebControls.HyperLink GetHyperLink()
        {
            HyperLink link = null;

            if (this.LinkTo != CSLinkLinkTo.Nothing)
            {
                CSLink csLink = this.DataSource as CSLink;

                if (csLink != null)
                {
                    switch (this.LinkTo)
                    {
                        case CSLinkLinkTo.Link:
                            link = new HyperLink();

                            //if (!Globals.IsNullorEmpty(this.LinkCssClass))
                            //    link.CssClass = this.LinkCssClass;

                            if (!Globals.IsNullorEmpty(this.LinkId))
                                link.Attributes.Add("id", this.LinkId);

                            if (!Globals.IsNullorEmpty(csLink.Target))
                                link.Target = csLink.Target;

                            link.NavigateUrl = csLink.NavigateUrl;
                            break;
                    }
                }
            }

            return link;
        }

        #endregion

    }

    #region Related Enums

    /// <summary>
    /// Links related to CSLink objects.
    /// </summary>
    public enum CSLinkLinkTo
    {
        /// <summary>
        /// The CSLink's location.
        /// </summary>
        Link,

        /// <summary>
        /// Do not link to any location.
        /// </summary>
        Nothing
    }

    #endregion

}
